package com.moneyanalyzer.adapter;

import java.util.ArrayList;
import java.util.List;

import com.moneyanalyzer.R;
import com.moneyanalyzer.model.Budget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

public class BudgetAdapter extends BaseAdapter {
	private Context context;
	private List<Budget> budgets;
	
	public BudgetAdapter(Context context, List<Budget> budgets){
		this.context = context;
		this.budgets = budgets;
	}

	@Override
	public int getCount() {
		return budgets.size();
	}

	@Override
	public Object getItem(int position) {
		return budgets.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup viewGroup) {
		Budget budget = budgets.get(position);
		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View itemView = inflater.inflate(R.layout.item_list_budget, null);
		ProgressBar progressBar = (ProgressBar)itemView.findViewById(R.id.progressBar);
		TextView nameBudget = (TextView)itemView.findViewById(R.id.nameBudget);
		TextView consumedBudget = (TextView)itemView.findViewById(R.id.consumedBudget);
		TextView categoryBudget = (TextView)itemView.findViewById(R.id.categoryBudget);
		progressBar.setMax((int)budget.getValue());
		progressBar.setProgress((int)budget.getConsumed());
		nameBudget.setText(budget.getName());
		consumedBudget.setText(String.format("R$%.02f", budget.getConsumed()));
		categoryBudget.setText(budget.getCategory().getName());
		return itemView;
	}
	
	public void setMyList(List<Budget> budgets){
		if(budgets != null && budgets.size() > 0){
			this.budgets = budgets;
		}else{
			this.budgets = new ArrayList<Budget>();
		}
		this.notifyDataSetChanged();
	}

}
